package com.windgo.controller.admin;

import com.github.pagehelper.PageInfo;
import com.windgo.bean.MarketRole;
import com.windgo.bean.MarketStorage;
import com.windgo.bean.bo.PermissionsBO;
import com.windgo.bean.bo.RoleCreateBO;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.BaseRespVo;
import com.windgo.bean.common.CommonData;
import com.windgo.bean.vo.GPermissionVO;
import com.windgo.bean.vo.RespInfoVO;
import com.windgo.bean.vo.RoleOptionsVO;
import com.windgo.bean.vo.RolePermissionVO;
import com.windgo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author doublec
 * @since 2022/10/12 23:08
 */

@RestController
@RequestMapping("admin/role")
public class AdminRoleController {
    @Autowired
    AdminService adminService;

    @RequestMapping("list")
    public BaseRespVo<CommonData<MarketRole>> listRole(BasePageInfo basePageInfo,String name){
        CommonData<MarketRole> commonData = new CommonData<>();
        PageInfo<MarketRole> pageInfo = adminService.listRole(basePageInfo,name);
        int total = Long.valueOf(pageInfo.getTotal()).intValue();
        int pages = pageInfo.getPages();
        Integer limit = basePageInfo.getLimit();
        int pageNum = pageInfo.getPageNum();
        List<MarketRole> list = pageInfo.getList();
        commonData.setTotal(total);
        commonData.setPages(pages);
        commonData.setLimit(limit);
        commonData.setPage(pageNum);
        commonData.setList(list);
        return BaseRespVo.ok(commonData);
    }

    @RequestMapping("create")
    public BaseRespVo<MarketRole> createRole(@RequestBody RoleCreateBO createBO){
        MarketRole marketRole = new MarketRole();
        int number = adminService.createRole(createBO);
        Integer id = marketRole.getId();
        MarketRole marketRoleInfo = adminService.queryMarketRole(id);
        return BaseRespVo.ok(marketRoleInfo);
    }

    @RequestMapping("update")
    public RespInfoVO updateRole(@RequestBody MarketRole marketRole){
        int affectedRows = adminService.updateRole(marketRole);
        if (affectedRows == 1){
            return RespInfoVO.ok();
        }
        return new RespInfoVO(505,"失败");
    }

    @RequestMapping("delete")
    @Transactional
    public RespInfoVO deleteStorage(@RequestBody MarketRole marketRole) {
        if (marketRole.getId() != 1){
            int number = adminService.deleteRole(marketRole);
            return RespInfoVO.ok();
        }
        return RespInfoVO.ok(404,"超级管理员不可被删除");
    }

    @RequestMapping("options")
    public BaseRespVo<CommonData<RoleOptionsVO>> optionsRole(){
        CommonData<RoleOptionsVO> commonData = new CommonData<>();
        List<RoleOptionsVO> list = adminService.optionsRole();
        commonData.setPages(1);
        commonData.setPage(1);
        commonData.setList(list);
        commonData.setTotal(list.size());
        commonData.setLimit(list.size());
        return BaseRespVo.ok(commonData);
    }

    @RequestMapping(value = "permissions",method = RequestMethod.GET)
    public BaseRespVo<RolePermissionVO> permissions(String roleId){
         RolePermissionVO rolePermissionVO = adminService.queryPermission(roleId);
        BaseRespVo<RolePermissionVO> rolePermissionVOBaseRespVo = new BaseRespVo<>();
        rolePermissionVOBaseRespVo.setErrno(0);
        rolePermissionVOBaseRespVo.setData(rolePermissionVO);
        rolePermissionVOBaseRespVo.setErrmsg("成功");
        return rolePermissionVOBaseRespVo;
    }

    @RequestMapping(value = "permissions",method = RequestMethod.POST)
    public BaseRespVo permissions(@RequestBody PermissionsBO permissionsBO){
        if (permissionsBO.getRoleId() != 1){
            int affectedRows = adminService.insertPermissions(permissionsBO);
            return BaseRespVo.ok(0);
        }
        return BaseRespVo.ok(404,"超级管理员权限不可被更改");

    }



}